package com.handpay.nocardpay.baselibrary.setting.view.fragment;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.handpay.nocardpay.baselibrary.R;
import com.handpay.nocardpay.baselibrary.account.model.bean.Account;
import com.handpay.nocardpay.baselibrary.account.model.bean.UserLoginValidateBean;
import com.handpay.nocardpay.baselibrary.account.model.bean.VerificationCodeBean;
import com.handpay.nocardpay.baselibrary.account.presenter.AccountPresenterImpl;
import com.handpay.nocardpay.baselibrary.account.presenter.VerfcodePresenterImpl;
import com.handpay.nocardpay.baselibrary.account.view.ILoginView;
import com.handpay.nocardpay.baselibrary.account.view.ISendVerfCodeView;
import com.handpay.nocardpay.baselibrary.account.view.IUserLoginValidateView;
import com.handpay.nocardpay.baselibrary.commbase.BaseProtocolKey;
import com.handpay.nocardpay.baselibrary.commbase.view.fragment.BaseFragment;
import com.handpay.nocardpay.baselibrary.config.NoCardPayConfig;
import com.handpay.nocardpay.baselibrary.config.SecureManager;
import com.handpay.nocardpay.baselibrary.utils.AccountUtils;
import com.handpay.nocardpay.baselibrary.utils.ClientEngine;
import com.handpay.nocardpay.baselibrary.utils.CommonUtils;
import com.handpay.nocardpay.baselibrary.utils.DialogUtils;
import com.handpay.nocardpay.baselibrary.utils.NoCardPayConstant;

import java.util.HashMap;

/**
 * Created by lyang on 2017/10/18.
 */

public class ForgetGesturePWDFragment extends BaseFragment implements View.OnClickListener, ISendVerfCodeView, IUserLoginValidateView {
    private Button btn_submit;
    private TextView btn_getcode;
    private TextView old_phone_tv;
    private VerfcodePresenterImpl verfcodePresenter;
    private AccountPresenterImpl mAccountPresenter;
    private CountDownTimer mCountDownTimer;

    private AppCompatEditText et_code;

    public static ForgetGesturePWDFragment newInstance() {
        Bundle args = new Bundle();
        ForgetGesturePWDFragment fragment = new ForgetGesturePWDFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_forget_gesture_pwd;
    }

    @Override
    public void initView(View view) {
        Toolbar mToolbar = (Toolbar) view.findViewById(R.id.toolbar);
        initToolbar(mToolbar, true);
        btn_submit = (Button) view.findViewById(R.id.btn_submit);
        btn_getcode = (TextView) view.findViewById(R.id.btn_getcode);
        old_phone_tv = (TextView) view.findViewById(R.id.old_phone_tv);
        et_code = (AppCompatEditText) view.findViewById(R.id.et_code);
    }

    @Override
    public String getFragmentTitle() {
        return _mActivity.getString(R.string.forget_gesture_pwd);
    }

    @Override
    public void initListener() {
        btn_submit.setOnClickListener(this);
        btn_getcode.setOnClickListener(this);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mAccountPresenter = new AccountPresenterImpl(_mActivity);
        mAccountPresenter.setUserLoginValidateView(this);
        verfcodePresenter = new VerfcodePresenterImpl(_mActivity);
        verfcodePresenter.setSendVerfCodeView(this);
        String phone = AccountUtils.getAccountPhone(_mActivity);
        old_phone_tv.setText(CommonUtils.formatPhoneNumber(phone));

        mCountDownTimer = new CountDownTimer(NoCardPayConstant.MILLISINFUTURE, NoCardPayConstant.COUNTDOWNINTERVAL) {
            @Override
            public void onTick(long millisUntilFinished) {
                btn_getcode.setText((millisUntilFinished / 1000) + _mActivity.getString(R.string.letter_getVefCode));
            }

            @Override
            public void onFinish() {
                btn_getcode.setEnabled(true);
                btn_getcode.setText(_mActivity.getString(R.string.getvercode));
            }
        };
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_submit) {
            HashMap<String, Object> param = new HashMap<String, Object>();
            String phone = AccountUtils.getAccountPhone(_mActivity);
            param.put(BaseProtocolKey.MOBILE, SecureManager.getInstance().des(phone, 1, null));
            String code = et_code.getText().toString();
            param.put(BaseProtocolKey.VERIFYCODE, SecureManager.getInstance().des(code, 1, null));
            param.put(BaseProtocolKey.ACTION, NoCardPayConstant.AUTHCODE_ACTION_RESET_GESTURE_PWD);
            mAccountPresenter.getUserLoginValidate(param, UserLoginValidateBean.class, true);
        } else if (v.getId() == R.id.btn_getcode) {
            btn_getcode.setEnabled(false);
            mCountDownTimer.start();
            HashMap<String, Object> param = new HashMap<String, Object>();
            param.put(BaseProtocolKey.ACCOUNT, SecureManager.getInstance().des(AccountUtils.getAccountPhone(_mActivity), 1, null));
            param.put(BaseProtocolKey.ACTION, NoCardPayConstant.AUTHCODE_ACTION_RESET_GESTURE_PWD);
            param.put(BaseProtocolKey.PHONE, SecureManager.getInstance().des(AccountUtils.getAccountPhone(_mActivity), 1, null));
            if (NoCardPayConfig.isNoCardPay) {
                param.put(BaseProtocolKey.CHANNEL, ClientEngine.getInstance().CHANNEL);
            } else {
                param.put(BaseProtocolKey.CHANNEL, ClientEngine.getInstance().FASTBILLCHANNEL);
            }
            verfcodePresenter.getVerificationCode(param, VerificationCodeBean.class, true);
        }
    }

    @Override
    public void showSendVerfCodeSuccess() {
        DialogUtils.showToast(_mActivity, _mActivity.getString(R.string.send_verfcode_success));
    }

    @Override
    public void validateSuccess() {
        setFragmentResult(SettingGesturePWDFragment.RESULT_VERIFY_OTP_SUCCESS, null);
        pop();
    }

    @Override
    public void validateError(String message) {
        DialogUtils.showToast(_mActivity, message);

    }
}
